<template>
  <!-- 菜品管理 -->
  <div class="dish-management">
    <div class="header">
      <el-input v-model="input" placeholder="请输入内容" @keyup.enter.native="init" clearable>
        <i slot="prefix" class="el-input__icon el-icon-search" @click="init"></i>
      </el-input>
      <div>
        <el-link type="danger" @click="DeleteDishes">批量删除</el-link>
        <el-divider direction="vertical"></el-divider>
        <el-link type="primary" @click="EnableDishes">批量启售</el-link>
        <el-divider direction="vertical"></el-divider>
        <el-link type="danger" @click="IsableDishes">批量停售</el-link>

        <el-button type="warning" icon="el-icon-plus"
          @click="$router.push({ name: 'AddCourse', query: { isEdit: '0' } })">增加菜品</el-button>
      </div>
    </div>
    <el-table :data="tableData" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="name" label="菜品名称"> </el-table-column>
      <el-table-column prop="image" label="照片">
        <template v-slot="{ row }">
          <el-image style="width: 50px; height: 50px" :src="`${env}/common/download?name=${row.image}`"
            :preview-src-list="[`${env}/common/download?name=${row.image}`]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="categoryName" label="菜品分类"> </el-table-column>
      <el-table-column prop="price" label="售价">
        <template v-slot="{ row }"> ￥{{ Number(row.price) / 100 }} </template>
      </el-table-column>
      <el-table-column prop="status" label="售卖状态">
        <template v-slot="{ row }">
          {{ row.status ? "启售" : "停售" }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link type="primary" @click="
            $router.push({
              name: 'EditCourse',
              query: { isEdit: '1', id: row.id },
            })">修改</el-link>
          <el-divider direction="vertical"></el-divider>

          <el-popconfirm title="确认修改菜品的状态?" @onConfirm="handleChangeStatus(row)">
            <el-link type="danger" slot="reference">{{
              row.status ? "停售" : "启售"
            }}</el-link>
          </el-popconfirm>
          <el-divider direction="vertical"></el-divider>

            <el-link type="primary" @click="handleDelete(row.id)">删除</el-link>


        </template>
      </el-table-column>
    </el-table>
    <div class="footer">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageInfo.page"
        :page-sizes="[10, 20, 30, 40]" :page-size="pageInfo.pageSize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { getCourse, changeStatus, deleteCourse } from "@/api/course";
export default {
  data() {

    return {
      env: process.env.VUE_APP_BASE_API,
      input: "",
      tableData: [],
      multipleSelection: [],
      pageInfo: {
        page: 1,
        pageSize: 10,
      },
      total: 0,
      ids: ""
    };
  },
  created() {
    this.init();
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    init() {
      getCourse({
        ...this.pageInfo,
        name: this.input,
      }).then((res) => {
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1;
      this.pageInfo.pageSize = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val;
      this.init();
    },
    async handleChangeStatus(row) {
      await changeStatus({
        id: row.id,
        status: +!row.status,
      });
      this.$message.success("改变成功");
      this.init();
    },
    async handleDelete(id) {
      await deleteCourse(id);
      this.$message.success("删除成功");
      this.init();
    },
    async DeleteDishes() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning("请选择要删除的菜品");
        return;
      }
      const ids = this.multipleSelection.map((item) => item.id);
      await deleteCourse(ids);
      this.$message("批量删除成功");
      this.init();
    },
    async EnableDishes() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning("请选择要删除的菜品");
        return;
      }
      const requests = this.multipleSelection.map((item) =>
        changeStatus({ id: item.id, status: 1 })
      );
      await Promise.all(requests);
      this.$message.success("批量启售成功");
      this.init();
    },
    async IsableDishes() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning("请选择要删除的菜品");
        return;
      }
      const requests = this.multipleSelection.map((item) =>
        changeStatus({ id: item.id, status: 0 })
      );
      await Promise.all(requests);
      this.$message.success("批量停售成功");
      this.init();
    }
  },
};
</script>

<style lang="scss" scoped>
.dish-management {
  padding: 33px;

  .header {
    display: flex;
    justify-content: space-between;

    .el-link {
      margin: 0px 10px;
    }

    .el-input {
      width: 250px;

      .el-icon-search {
        cursor: pointer;
      }
    }
  }

  .el-table {
    margin-top: 20px;
  }

  .footer {
    margin-top: 30px;
    display: flex;
    justify-content: center;
  }
}
</style>
